package com.example.springboot.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.base.Result;
import com.example.springboot.mapper.FolderInfoMapper;
import com.example.springboot.mapper.RootDirectoryInfoMapper;
import com.example.springboot.mapper.UserInfoMapper;
import com.example.springboot.pojo.FolderInfo;
import com.example.springboot.pojo.RootDirectoryInfo;
import com.example.springboot.pojo.UserInfo;
import com.example.springboot.service.UserInfoService;
import com.example.springboot.utils.JWTUtil;
import com.example.springboot.utils.HdfsUtil;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 实现接口并返回提示信息
 */

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private FolderInfoMapper folderInfoMapper;

    @Resource
    private RootDirectoryInfoMapper rootDirectoryInfoMapper;

    @Override
    public Result userLogin(String username, String password) {
        try {
            Map<String, Object> folderMap = userInfoMapper.selectJoinMap(new MPJLambdaWrapper<>()
                    .select(UserInfo::getUserName, UserInfo::getUserPwd)
                    .select(RootDirectoryInfo::getFolderId)
                    .leftJoin(RootDirectoryInfo.class, RootDirectoryInfo::getUserId, UserInfo::getUserId)
                    .eq(UserInfo::getUserName, username));
            if (password.equals(folderMap.get("userPwd"))) {
                Map<String, String> map = new HashMap<>();
                map.put("userName", (String) folderMap.get("userName"));
                String token = JWTUtil.getToken(map);
                map.remove("userName");
                map.put("token", token);
                map.put("folderId", (String) folderMap.get("folderId"));
                return Result.success("登录成功", map);
            }
            return Result.error("密码错误");
        } catch (Exception e) {
            return Result.error("用户不存在");
        }
    }

    @Override
    public Result userRegister(String userName, String password) {
        UserInfo user = new UserInfo();
        String userId = IdUtil.simpleUUID();
        user.setUserId(userId);
        user.setUserName(userName);
        user.setUserPwd(password);
        userInfoMapper.insert(user);
        String hdfsPath = "/cloud_disk/" + userName;
        FolderInfo folderInfo = new FolderInfo();
        String folderId = IdUtil.simpleUUID();
        folderInfo.setUserId(userId);
        folderInfo.setFolderId(folderId);
        folderInfo.setFolderName(userName);
        folderInfo.setFolderUrl(hdfsPath);
        folderInfo.setFolderDel(1);
        folderInfoMapper.insert(folderInfo);
        RootDirectoryInfo directoryInfo = new RootDirectoryInfo();
        directoryInfo.setUserId(userId);
        directoryInfo.setFolderId(folderId);
        directoryInfo.setRootDirectoryId(IdUtil.fastUUID());
        rootDirectoryInfoMapper.insert(directoryInfo);
        HdfsUtil.createFolder(hdfsPath);
        return Result.success("注册成功");
    }


    public Result token(String token) {
        String userName = JWTUtil.getValue(token, "userName");
        return Result.success(userName);
    }

}
